package middleware

import (
	"github.com/gin-gonic/gin"
	"strings"
	"vip_video/internal"
	"vip_video/internal/library"
)

func SafeApiMiddleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		response := library.NewResponse(context)

		//请求地址
		request := context.Request

		path := request.URL.Path //路径
		method := request.Method //方法
		version := request.Header.Get("X-Version")
		platform := request.Header.Get("X-Platform")
		timestamp := request.Header.Get("X-RequestTime")
		appEncryption := request.Header.Get("X-Encryption")

		if !strings.EqualFold(appEncryption, internal.Md5Str(path+method+platform+version+timestamp)) {
			response.ErrorAbort("!禁止访问!请不要运用你的小聪明!")
			return
		}

		context.Next()
	}
}
